package com.sun.officeoa.dao;

import com.sun.officeoa.bean.Department;
import com.sun.officeoa.bean.Employee;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by sang on 2017/8/17.
 */
@Repository
public class DepartmentDao {

    @Resource
    private JdbcTemplate jdbcTemplate;


    public int updateDepById(String name, int id) {
        String sql = "UPDATE department SET departmentname=? WHERE departmentid=?";
        return jdbcTemplate.update(sql, name, id);

    }

    public int insert(String name) {
        String sql = "insert into department (departmentname) values (?);";
        return jdbcTemplate.update(sql,name);
    }

    public int deleteDepById(int id) {
            String sql = "DELETE FROM department WHERE departmentid=?";
            return jdbcTemplate.update(sql,id);
    }

    public List<Department> getAllDepartment() {
        List<Department> users = null;
        String sql ="select * from department;";
        RowMapper<Department> rowMapper = new BeanPropertyRowMapper<Department>(Department.class);
        users = jdbcTemplate.query(sql,rowMapper);
        return (users != null && users.size() > 0) ? users : null;
    }


}
